package LockTest;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class CountDownLatchTest {
    private final static Logger logger = LoggerFactory.getLogger(CountDownLatchTest.class);

    @Test
    public void test() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(2);
        logger.info("threadMain start");
        new Thread(() -> {
            logger.info("threadA start");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            latch.countDown();
            logger.info("threadA end");
        }).start();
        new Thread(() -> {
            logger.info("threadB start");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            latch.countDown();
            logger.info("threadB end");
        }).start();
        logger.info("threadMain wait subThreads");
        latch.await();
        logger.info("threadMain end");
    }
}
